process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
const charset = require('superagent-charset');
const request = require('superagent');
charset(request);
var cheerio = require('cheerio');
var async = require('async');
var fs = require("fs");
var url = require('url');
var path=require('path');
var utils=require('./utils');
var email=require('./email');
var emptyFun=function () {};
function getBook(setting) {
    if(!setting.errorCallback)setting.errorCallback=function (str) {
        email('306679829@qq.com','error:'+str);
        console.error(str);
    };
    if(!setting.resultCallback)setting.resultCallback=emptyFun;
    if(!setting.logCallback)setting.logCallback=function (log) {console.log(log);};
    if(!setting.url){
        setting.errorCallback('未找到url参数');
        return;
    }
    var i = 0, title = 'default', breakChar = setting.breakChar || "\n";
    var urlDomainParse = url.parse(setting.url);
    var array = [];
    async.waterfall(
        [
            function (callback) {
                setting.logCallback('获取数据源：'+setting.url);
                request.get(setting.url)
                    .end(function (err, res) {
                        if (err) {
                            console.error(err);
                            callback(err);
                            return;
                        }
                        var charset;
                        var $ = cheerio.load(res.text);
                        if($('meta[charset]').attr('charset')){
                            callback(null, {charset: $('meta[charset]').attr('charset')});
                            return;
                        }
                        $('meta[http-equiv]').each(function(){
                            if(this.attribs['http-equiv'].toUpperCase()==='CONTENT-TYPE'){
                                this.attribs.content.split(';').forEach(function(d){
                                    var g=d.trim().split('=');
                                    if(g[0]==='charset'){
                                        charset=g[1];
                                    }
                                })
                            }
                        })
                        callback(null, {charset: charset||res.charset});
                    });
            }
        ],
        function (err, result) {
            if (!!err){
                setting.errorCallback('获取来源数据错误');
                return;
            }
            if(setting.isServer){
                setting.charset = result.charset || 'gbk';
            }else{
                setting.charset = setting.charset || result.charset || 'gbk';
            }
            request.get(setting.url)
                .charset(setting.charset)
                .end(function (err, res) {
                    if (err) {
                        setting.errorCallback('获取来源数据错误');
                        console.error(err);
                        return;
                    }
                    res.text = menuExcludeSpecial(res.text);
                    var $ = cheerio.load(res.text);
                    title = setting.titleText || $(setting.title).text();
                    (setting.titleMoreText || []).forEach(function (d) {
                        title = title.replace(new RegExp(d,"gm"), '');
                    });
                    if(!setting.isRefresh&&fs.existsSync('./resource/' + title+ '.txt')){
                        console.log('已生成：'+title);
                        setting.resultCallback(title);
                        return;
                    }

                    if ($(setting.menu).length === 0) {
                        setting.errorCallback("未找到菜单数据");
                        return;
                    }
                    var repeat = {};
                    $(setting.menu).each(function (i) {
                        var zhis = this;
                        if ((function () {
                                if (!setting.menuRule)return true;
                                for (var i = 0; i < setting.menuRule.length; i++) {
                                    if (!!setting.menuMoreHtml) $(zhis).find(setting.menuMoreHtml).remove();
                                    if ($(zhis).text().indexOf(setting.menuRule[i]) >= 0)return false;
                                }
                                return true;
                            })()) {
                            if (!!repeat[$(this).text()])return true;
                            repeat[$(this).text()] = true;
                            setting.logCallback($(this).text());
                            setting.domain = urlDomainParse.protocol + "//" + urlDomainParse.host;
                            if ($(this).attr('href').indexOf('/') != 0) {
                                setting.domain += urlDomainParse.pathname;
                            }
                            array.push(getZhang(url.resolve(setting.domain, $(this).attr('href')), $(this).text(), i,setting));
                        }
                    });

                    page(array, 100,setting);
                    //return;
                    /*async.parallel(array, function (err, data) {

                     for (var i = 0; i < data.length; i++) {
                     str += "\r\n" + data[i];
                     }
                     //console.log(str);
                     setFile(str);
                     });*/
                });
        }
    );
    function setFile(str,options) {
        str=title+str;
        fs.writeFile('./resource/' + title + '.txt', new Buffer(str), function (err) {
            if (err){
                options.errorCallback('保存文件错误');
                throw err;
                return;
            }
            setting.logCallback(title + '-----生成完毕！');
            options.resultCallback(title);
        });
    }

    function getZhang(url, ti, index,setting) {
        return function (callback) {
            request.get(url)
                .charset(setting.charset)
                .timeout(setting.timeout)
                .end(function (err, res) {
                    if (!!err) {
                        if(!setting.errorIndex)setting.errorIndex=0;
                        if(setting.errorIndex>=30){
                            console.error(err);
                            console.log('错误次数超过30次 url:'+url);
                            callback(null, "");
                            return;
                        }
                        setting.errorIndex+=1;
                        getZhang(url, ti, index,setting)(callback);
                        return;
                    }
                    //var $ = cheerio.load(res.text);
                    i++;
                    setting.logCallback(title + " " + index + " " + i + " " + ti);
                    var $ = cheerio.load(res.text);
                    if (!!setting.loadFun) setting.loadFun($);
                    if ($(setting.content).html() == null) {
                        console.log($(setting.content));
                        callback('没有找到对应的正文节点', "");
                        return;
                    }
                    var selectDom=$(setting.content);
                    var lastSelect=selectDom.eq(selectDom.length-1);
                    var content = cheerio.load(lastSelect.html().replace(/<br>/g, breakChar)).root();
                    if (!!setting.loadMoreHtml) content.find(setting.loadMoreHtml).remove();
                    var data = ti + breakChar + excludeSpecial(content.text(), setting);
                    callback(null, data);
                });
        }
    }

    function page(array, size,setting) {
        var count = Math.ceil(array.length / size);
        var arraySeries = [];
        for (var i = 1; i <= count; i++) {
            arraySeries.push((function (page, size, bool) {
                console.log((page - 1) * size + " " + (bool ? undefined : page * size));
                return function (callback) {

                    async.parallel(array.slice((page - 1) * size, bool ? undefined : page * size), function (err, data) {
                        callback(err, data);
                    });
                };
            })(i, size, i == count));
        }
        async.series(arraySeries, function (err, data) {
            if(!!err) {
                setting.errorCallback(err);
                return;
            }
            var str = "";
            for (var i = 0; i < data.length; i++) {
                for (var j = 0; j < data[i].length; j++) {

                    str += "\r\n" + data[i][j];
                }
            }
            //console.log(str);
            setFile(str,setting);
        });
    }
}

var excludeSpecial = function (s, setting) {
    s = s.replace(/<[^>]+>/g, "");
    s = s.replace(/\n/g, "\n");
    s = s.replace(/&nbsp;/g, "");
    s = s.replace('()$()$(小)$(说)$().---.高速！', '');
    s = s.replace('~@~！中@！~vvww..', '');
    s = s.replace('#中 @.', '');
    s = s.replace('````中`` .~.', '');
    s = s.replace('````中``', '');
    s = s.replace(')（中& .', '');
    s = s.replace('$nbsp;', '');
    s = s.replace('(www.92txt.net 就爱网)', '');
    s = s.replace('<>天才壹秒記住『→網.』，為您提供精彩小說閱讀。', '');
    s = s.replace('一秒记住【笔♂趣→阁 WWW.BiQiuGe.Com】，精彩小说无弹窗免费阅读！', '');
    if (!!setting.contentReplace) {
        for (var i = 0; i < setting.contentReplace.length; i++) {
            s = s.replace(setting.contentReplace[i], '');
        }
    }
    return s;
};
var menuExcludeSpecial = function (s) {
    s = s.replace(/<!--[^>]+>/g, '');
    s = s.replace(/<[^>]+\-\-\!>/g, '');
    return s;
};
module.exports = getBook;
